﻿namespace Leetcode.N0024;
public class Solution
{
    public ListNode SwapPairs(ListNode head)
    {
        if (head?.next == null)
        {
            return head;
        }
        var node1 = head;
        var node2 = head.next;

        head = node2;
        var tmp = node2.next;
        node2.next = node1;
        node1.next = tmp;
        node2 = tmp;

        while (node2?.next != null)
        {
            node1.next = node2.next;
            node2.next = node2.next.next;
            node1.next.next = node2;
            node1 = node2;
            node2 = node1?.next;

        }

        return head;
    }
}